<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;

use think\Db;

class Secondhandle2 extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}

	/**
	*二手车省份选择
	**/

	public function address()
	{
		$address = Db('area')->where('parent_id',0)->select();
		$this->doif($address);
	}

	/**
	*二手车地址地级市选择
	*@param  int id 		  省份的id
	**/

	public function addressCity()
	{
		$id = $_GET['id'];
        if($id){
            $area_list = Db('area')->where('parent_id',$id)->select();
			$this->doif($area_list);
        }
	}

	/**
	*二手车车型分类的选择
	**/
	public function type()
	{
            $list = Db('used_classify')->where('pid',1)->select();
			$this->doif($list);
	}

	/**
	*二手车品牌
	*@param  int tid 		  车型分类的tid
	**/
	public function brand()
	{
		$tid = $_GET['tid'];
        $list = Db('used_classify')->select();
        $i = 0;
        for (; $i < count($list); $i++) {
        	$stack = explode(',',$list[$i]['pid']);
        	if(count($stack) > 1){
				$num1 = array_shift($stack);
        		$num2 = array_shift($stack);
        		if ($num2 == 0 && $num1 == $tid) {
        			$arr[] = $list[$i];
        		}	
        	}        	
        }
		$this->doif($arr);
	}

	/**
	*二手车车系
	*@param  int tid 		  车型的tid
	*@param  int bid 		  车型品牌的bid
	**/
	public function series()
	{
		$tid = $_GET['tid'];
		$bid = $_GET['bid'];
        $list = Db('used_classify')->select();
        $i = 0;
        for (; $i < count($list); $i++) {
        	$stack = explode(',',$list[$i]['pid']);
        	if(count($stack) > 1){
				$num1 = array_shift($stack);
        		$num2 = array_shift($stack);
        		$num3 = array_shift($stack);
        		if ($num2 == $bid && $num1 == $tid && $num3 == 0) {
        			$arr[] = $list[$i];
        		}	
        	}        	
        }
		$this->doif($arr);
	}

	/**
	*二手车型号
	*@param  int tid 		  车型的tid
	*@param  int bid 		  车型品牌的bid
	*@param  int sid 		  车系sid
	**/
	public function version()
	{
		$tid = $_GET['tid'];
		$bid = $_GET['bid'];
		$sid = $_GET['sid'];
        $list = Db('used_classify')->select();
        $i = 0;
        for (; $i < count($list); $i++) {
        	$stack = explode(',',$list[$i]['pid']);
        	if(count($stack) > 1){
				$num1 = array_shift($stack);
        		$num2 = array_shift($stack);
        		$num3 = array_shift($stack);
        		if ($num2 == $bid && $num1 == $tid && $num3 == $sid) {
        			$arr[] = $list[$i];
        		}	
        	}        	
        }
		$this->doif($arr);
	}

	/**
	*公共方法:将数组进行判断
	*@param  $arr		  要判断的数组
	**/
	public function doif($arr)
	{
		if($arr){
            $this->apiSuccess($arr);
        }else{
            $this->apiError('没有查到信息');
        }
	}


	/**
	*二手车品牌
	**/
	public function onlybrand()
	{
		//取出品牌数组
        $list = Db('used_classify')->select();
        $i = 0;
        for (; $i < count($list); $i++) {
        	$stack = explode(',',$list[$i]['pid']);
        	if(count($stack) > 1){
				$num1 = array_shift($stack);
        		$num2 = array_shift($stack);
        		if ($num2 == 0) {
        			$arr[] = $list[$i];
        		}	
        	}        	
        }
        //品牌名去重复
        $j = 0;
        for(;$j<count($arr);$j++){
        	$name[] = $arr[$j]['name'];

        }
        $arr1 = array_unique($name);
        //根据品牌名字查询品牌数组
        foreach ($arr1 as $val) {
        	$brand = Db('used_classify')->where('name',$val)->find();
        	$brands[] = $brand;
        }
        $this->doif($brands);
	}
}